package net.wangds.auth.grant.entity;

import lombok.NoArgsConstructor;
import net.wangds.auth.module.GrantOwnerTypeEnum;
import net.wangds.auth.module.PrivilegeActionEnum;
import net.wangds.auth.module.Resource;
import net.wangds.auth.module.impls.defaults.DefaultGrant;
import net.wangds.data.RemovedEnum;

import javax.persistence.Entity;
import javax.persistence.Table;

@Entity
@Table(name="auth_grant")
@NoArgsConstructor(force = true)
public class GeneralGrantEntity extends DefaultGrant {
    private static final long serialVersionUID = 0;

    public static GeneralGrantEntity of(GrantOwnerTypeEnum ownerType, String ownerId, Resource res, int privilege) {
        GeneralGrantEntity g = new GeneralGrantEntity();

        g.setOwnerType(ownerType);
        g.setOwnerId(ownerId);
        g.setAction(privilege);

        g.setSchema(res.getSchema());
        g.setSys(res.getSys());
        g.setModule(res.getModule());
        g.setFeature(res.getFeature());
        g.setActivity(res.getActivity());

        g.setResId(res.getId());

        g.setAction(PrivilegeActionEnum.ALL_GRANT_ALL.toVal()&privilege);

        g.setRemoved(RemovedEnum.NORMAL);

        return g;
    }
}
